package com.youyi.yyviewsdklibrary.Dialog.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class BackgroundExecutor {
    public static Executor DEFAULT_EXECUTOR;
    private static Executor executor;
    private static final List<Task> tasks = new ArrayList();

    static {
        ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2);
        DEFAULT_EXECUTOR = newScheduledThreadPool;
        executor = newScheduledThreadPool;
    }

    private static Future<?> directExecute(Runnable runnable, int i) {
        if (i > 0) {
            Executor executor2 = executor;
            if (executor2 instanceof ScheduledExecutorService) {
                return ((ScheduledExecutorService) executor2).schedule(runnable, (long) i, TimeUnit.MILLISECONDS);
            }
            throw new IllegalArgumentException("The executor set does not support scheduling");
        }
        Executor executor3 = executor;
        if (executor3 instanceof ExecutorService) {
            return ((ExecutorService) executor3).submit(runnable);
        }
        executor3.execute(runnable);
        return null;
    }

    public static synchronized void execute(Task task) {
        synchronized (BackgroundExecutor.class) {
            Future<?> future = null;
            if (task.serial == null || !hasSerialRunning(task.serial)) {
                task.executionAsked = true;
                future = directExecute(task, task.remainingDelay);
            }
            if (!(task.id == null && task.serial == null)) {
                task.future = future;
                tasks.add(task);
            }
        }
    }

    public static void execute(final Runnable runnable, String str, int i, String str2) {
        execute((Task) new Task(str, i, str2) {
            /* class com.youyi.yyviewsdklibrary.Dialog.util.BackgroundExecutor.AnonymousClass1 */

            @Override // com.youyi.yyviewsdklibrary.Dialog.util.BackgroundExecutor.Task
            public void execute() {
                runnable.run();
            }
        });
    }

    public static void execute(Runnable runnable, int i) {
        directExecute(runnable, i);
    }

    public static void execute(Runnable runnable) {
        directExecute(runnable, 0);
    }

    public static void execute(Runnable runnable, String str, String str2) {
        execute(runnable, str, 0, str2);
    }

    public static void setExecutor(Executor executor2) {
        executor = executor2;
    }

    public static synchronized void cancelAll(String str, boolean z) {
        synchronized (BackgroundExecutor.class) {
            for (int size = tasks.size() - 1; size >= 0; size--) {
                List<Task> list = tasks;
                Task task = list.get(size);
                if (str.equals(task.id)) {
                    if (task.future != null) {
                        task.future.cancel(z);
                        if (!task.managed.getAndSet(true)) {
                            task.postExecute();
                        }
                    } else if (!task.executionAsked) {
                        list.remove(size);
                    }
                }
            }
        }
    }

    private static boolean hasSerialRunning(String str) {
        for (Task task : tasks) {
            if (task.executionAsked && str.equals(task.serial)) {
                return true;
            }
        }
        return false;
    }

    /* access modifiers changed from: private */
    public static Task take(String str) {
        int size = tasks.size();
        for (int i = 0; i < size; i++) {
            List<Task> list = tasks;
            if (str.equals(list.get(i).serial)) {
                return list.remove(i);
            }
        }
        return null;
    }

    public static abstract class Task implements Runnable {
        private boolean executionAsked;
        private Future<?> future;
        private String id;
        private AtomicBoolean managed = new AtomicBoolean();
        private int remainingDelay;
        private String serial;
        private long targetTimeMillis;

        public abstract void execute();

        public Task(String str, int i, String str2) {
            if (!"".equals(str)) {
                this.id = str;
            }
            if (i > 0) {
                this.remainingDelay = i;
                this.targetTimeMillis = System.currentTimeMillis() + ((long) i);
            }
            if (!"".equals(str2)) {
                this.serial = str2;
            }
        }

        public void run() {
            if (!this.managed.getAndSet(true)) {
                try {
                    execute();
                } finally {
                    postExecute();
                }
            }
        }

        /* access modifiers changed from: private */
        /* access modifiers changed from: public */
        private void postExecute() {
            Task take;
            if (this.id != null || this.serial != null) {
                synchronized (BackgroundExecutor.class) {
                    BackgroundExecutor.tasks.remove(this);
                    String str = this.serial;
                    if (!(str == null || (take = BackgroundExecutor.take(str)) == null)) {
                        if (take.remainingDelay != 0) {
                            take.remainingDelay = Math.max(0, (int) (this.targetTimeMillis - System.currentTimeMillis()));
                        }
                        BackgroundExecutor.execute(take);
                    }
                }
            }
        }
    }
}
